// pages/address_list/address_list.js
var app = getApp();
Page({
  data: {
    addressList: [],
  },

  onLoad: function (options) {

  },
  onShow: function (options) {
    this.getAddressListOptions();
  },


  getAddressListOptions: function () {
    var addressListOptions = {
      list_id: 'addressList',
      url: '/api/index.php?app=address&act=lists'
    }
    this.setData({
      scrollHeight: app.globalData.windowHeight - 110,
      addressListOptions: addressListOptions
    })
  },

  loadMoreData: function (e) {
    this.setData({
      addressList: e.detail.data
    })
  },

  /**
   * 选择默认地址
   */
  setDefaultAddress: function (e) {
    var targetId = e.currentTarget.dataset.target;
    app.Ajax({
      method: 'POST',
      url: '/api/index.php?app=address&act=status',
      data: {
        addr_id: targetId
      },
      success: (res) => {
        this.toastShow('设置成功');
        console.log(this.data.addressList);
        let newArr = this.data.addressList.slice();
        for (let item of newArr) {
          item.status = 0;
          if (item.addr_id == targetId) {
            item.status = 1
          }
        }
        this.setData({
          addressList: newArr
        })
      },
      fail: (err) => {
        this.toatShow('设置失败');
      }
    })
  },

  /**
   * 添加收货地址
   */
  addAddress: function (e) {
    wx.navigateTo({
      url: '../address_info/address_info',
    })
  },

  //编辑收货地址
  editAddress: function (e) {
    let targetId = e.currentTarget.dataset.idx;
    wx.navigateTo({
      url: '../address_info/address_info' + '?id=' + targetId,
    })
  },


  //点击删除地址
  delAddress: function (e) {
    let targetId = e.currentTarget.dataset.idx;
    let self = this;
    wx.showModal({
      title: '提示',
      content: '是否确定删除该收货地址',
      success: function (res) {
        if (res.confirm) {
          app.Ajax({
            method: 'POST',
            data: { addr_id: targetId },
            url: '/api/index.php?app=address&act=delete',
            success: (res) => {
              self.toastShow('删除成功');
              self.getAddressListOptions();
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  /**
   * 获取微信通讯地址
   */
  getWeixinAddress: function () {
    wx.chooseAddress({
      success: (res) => {
        console.log(res)
        //调用接口新增收货地址
        let formData = {
          province: res.provinceName,
          city: res.cityName,
          region: res.countyName,
          consignee: res.userName,
          address: res.detailInfo,
          phone: res.telNumber,
        };

        //发起请求
        app.Ajax({
          method: 'POST',
          url: '/api/index.php?app=address&act=add',
          data: formData,
          success: (res) => {
            //请求成功
            this.toastShow('新增成功');
          },
          fail: (err) => {
            console.log(err);
            this.toastShow(err);
          }
        })
      }
    })
  },

  /**
  * 显示报错信息
  */
  toastShow: function (message) {
    this.setData({
      message: message,
    })
  },
})

